<template>
  <div id="specSignHistory" class="history-container" style="display: flex;flex-direction: column;height: 100%;">
    <vxe-toolbar perfect class-name="history-top-tool-box">
      <template #buttons>
        <vxe-form :data="form" title-align="right" title-colon>
          <vxe-form-item title="条码号">
            <template #default="{ data }">
              <vxe-input v-model="data.reqNo" clearable @clear="findSpecSamplingHistoryList()"></vxe-input>
            </template>
          </vxe-form-item>
          <vxe-form-item title="标本号">
            <template #default="{ data }">
              <vxe-input v-model="data.sampleNos" clearable @clear="findSpecSamplingHistoryList()" placeholder="标本号范围 1,20,30~60"></vxe-input>
            </template>
          </vxe-form-item>
          <vxe-form-item title="检验日期">
            <template #default="{ data }">
              <vxe-input v-model="data.samplingTimeS" type="datetime" clearable transfer @clear="findSpecSamplingHistoryList()" @change="findSpecSamplingHistoryList" style="width: 160px"></vxe-input>
              <span class="margin-lr-xs">-</span>
              <vxe-input v-model="data.samplingTimeE" type="datetime" clearable transfer @clear="findSpecSamplingHistoryList()" @change="findSpecSamplingHistoryList" style="width: 160px"></vxe-input>
            </template>
          </vxe-form-item>
          <vxe-form-item title="报告单元">
            <template #default="{ data }">
              <SelectWorkGroup :url="'/api/rptGroup/findRptGroupList'" :businessType="'10'" @getSelectedId="getSelectedId"></SelectWorkGroup>
            </template>
          </vxe-form-item>
          <vxe-form-item>
            <vxe-button type="text" class="button-blue" icon="vxe-icon-in" content="查询" @click="findSpecSamplingHistoryList"></vxe-button>
            <vxe-button type="text" class="button-blue" icon="vxe-icon-in" content="分配孔位" @click="allSpecbox"></vxe-button>
          </vxe-form-item>
        </vxe-form>
      </template>
    </vxe-toolbar>

    <div class="specSignHistoryTable history-center-table-box" :style="{ height: 'calc(100% - 40px - 32px)',flex:'1 1 auto' }">
      <vxe-table ref="specSignHistoryTableRef" v-loading="loading" border align="left" height="auto" class="mytable-scrollbar" :data="specSignHistoryList" :row-config="{ isCurrent: true, isHover: true }" :keyboard-config="{ isArrow: true, isTab: true, isEdit: true }"  resizable show-overflow>

        <vxe-column type="checkbox" width="30px"></vxe-column>
        <vxe-column field="reqNo" title="条码号" width="100px">
          <template #default="{ row }">
            <span>{{ row.reqNo ? row.reqNo : '' }}</span>
          </template>
        </vxe-column>
        <vxe-column field="patType" title="病人来源" width="60">
          <template #default="{ row }">
            {{ row.patType === '1' ? '门诊' : row.patType === '2' ? '急诊' : row.patType === '3' ? '住院' : row.patType === '4' ? '体检' : '未知' }}
          </template>
        </vxe-column>
        <vxe-column field="sampleTypeNa" title="样本类型" width="60"></vxe-column>

        <vxe-column field="dep" title="申请科室" width="80">
          <template #default="{ row }">
            <span>{{ row.reqDeptNa ? row.reqDeptNa : '' }}</span>
          </template>
        </vxe-column>
        <vxe-column field="remark1" title="申请号" width="80"></vxe-column>
        <vxe-column field="patNo" title="病历号" width="100"></vxe-column>
        <vxe-column field="patNa" title="姓名" width="80"></vxe-column>
        <vxe-column field="sexNa" title="性别" width="60"></vxe-column>
        <vxe-column field="ageStr" title="年龄" width="80"></vxe-column>
        <vxe-column field="bedNo" title="床号" width="80"></vxe-column>
        <vxe-column field="reqPurpose" title="申请项目" min-width="180"></vxe-column>
        <vxe-column field="reqDoctorNa" title="申请人" width="80">
          <template #default="{ row }">
            <span>{{ row.reqDoctorNa ? row.reqDoctorNa : '' }}</span>
          </template>
        </vxe-column>
        <vxe-column field="reqTime" title="申请时间" width="120">
          <template #default="{ row }">
            <span>{{ row.reqTime ? row.reqTime : '' }}</span>
          </template>
        </vxe-column>

        <vxe-column field="signerNa" title="签收人" width="100" v-if="code === '70'">
          <template #default="{ row }">
            <span>{{ row.signerNa ? row.signerNa : '' }}</span>
          </template>
        </vxe-column>
        <vxe-column field="signTime" title="签收时间" width="120" v-if="code === '70'">
          <template #default="{ row }">
            <span>{{ row.signTime ? row.signTime : '' }}</span>
          </template>
        </vxe-column>
        <vxe-column field="samplingDoctorNa" title="采集人" width="100" v-if="code === '40'">
          <template #default="{ row }">
            <span>{{ row.samplingDoctorNa ? row.samplingDoctorNa : '' }}</span>
          </template>
        </vxe-column>
        <vxe-column field="samplingTime" title="采集时间" min-width="120" v-if="code === '40'">
          <template #default="{ row }">
            <span>{{ row.samplingTime ? row.samplingTime : '' }}</span>
          </template>
        </vxe-column>
        <vxe-column field="samplingDoctorNa" title="送出人" width="100" v-if="code === '50'">
          <template #default="{ row }">
            <span>{{ row.samplingDoctorNa ? row.samplingDoctorNa : '' }}</span>
          </template>
        </vxe-column>
        <vxe-column field="samplingTime" title="送出时间" min-width="120" v-if="code === '50'">
          <template #default="{ row }">
            <span>{{ row.samplingTime ? row.samplingTime : '' }}</span>
          </template>
        </vxe-column>
        <vxe-column field="samplingDoctorNa" title="送达人" width="80" v-if="code === '60'">
          <template #default="{ row }">
            <span>{{ row.samplingDoctorNa ? row.samplingDoctorNa : '' }}</span>
          </template>
        </vxe-column>
        <vxe-column field="samplingTime" title="送达时间" min-width="120" v-if="code === '60'">
          <template #default="{ row }">
            <span>{{ row.samplingTime ? row.samplingTime : '' }}</span>
          </template>
        </vxe-column>
      </vxe-table>
    </div>
  </div>
</template>
<script lang="ts" setup>
// 引入组件区 ==========
import { onMounted, ref, reactive, getCurrentInstance } from 'vue';
import { IsEmpty, formatDateTime } from '@/utils/common';
import { VxeTableEvents, VxeTableInstance } from 'vxe-table';
import VxePulldownSelection from '@/components/VxePulldownSelection.vue';
import {VxeMessage, VxeNotifyType} from "@/utils/CommonMessage";
import SelectWorkGroup from '../../lab_bus/components/SelectWorkGroup.vue';
import {GetRptGroupOptions} from "@/utils/SysOptionsHelper"; //报告单元引入下拉组件

// 方法定义区 =============
onMounted(() => {

});

// 全局变量定义区 ==========
let pageIndex = 1;
let totalCount = 0;
let loading = ref(false);
let specSignHistoryList = reactive([]); //列表
let currentRowIndex = 0;
const specSignHistoryTableRef = ref<VxeTableInstance<RowVO>>();
let { proxy }: any = getCurrentInstance();
let signState = [
  { value: null, label: '全部' },
  {
    value: true,
    label: '已接收'
  },
  {
    value: false,
    label: '未接收'
  }
];
let form = reactive({
  reqNo: '',
  sampleNos: '', //病历号
  patNa: '',
  samplingTimeS: formatDateTime(new Date().setTime(new Date().getTime() - 3600 * 1000 * 24 * 7), 'yyyy-MM-dd 00:00:00'),
  samplingTimeE: formatDateTime(new Date(), 'yyyy-MM-dd 23:59:59'),
  rptGroup: '',
  rptGroupName: '',
});
let code = ref('160');
let processAttributeList = reactive([]); //流程节点属性
let isBbth = ref(false);
function initPage(refData: any) {
  if (!IsEmpty(processAttributeList)) {
    processAttributeList.forEach((item) => {
      if (item.isRollback) {
        isBbth.value = true;
      }
    });
  }
}

const toggleSelectRow = (row: RowVO) => {
  const $table = specSignHistoryTableRef.value
  if ($table) {
    $table.toggleCheckboxRow(row)
  }
}

// 下拉报告单元获取报告单元id
function getSelectedId(selectedRadioId, selectedName) {
  form.rptGroup = selectedRadioId.value;
  form.rptGroupName = selectedName.value;
  findSpecSamplingHistoryList();
}
//获取列表
function findSpecSamplingHistoryList() {
  specSignHistoryList.length = 0;
  loading.value = true;
  const url = '/api/samsto/samstoSpecbox/findLabReqMainByKeepSampleState';
  const data = {
    state: code.value,
    reqNo: form.reqNo,
    sampleNos: form.sampleNos,
    rptGroup: form.rptGroup,
    samplingTimeS: !IsEmpty(form.samplingTimeS) ? form.samplingTimeS : null,
    samplingTimeE: !IsEmpty(form.samplingTimeE) ? form.samplingTimeE : null
  };

  proxy.$http
    .get(url, { params: data })
    .then((res: any) => {
      specSignHistoryList.push(...res.data);
      loading.value = false;
    })
    .catch((err: any) => {
      loading.value = false;
      proxy.$alert(err, '提示');
    });
}

let currentRow = reactive({});

defineExpose({ initPage, toggleSelectRow,findSpecSamplingHistoryList});

function allSpecbox(){
  const selectRows = (proxy.$refs.specSignHistoryTableRef as any).getCheckboxRecords();
  if (selectRows.length <= 0){
    VxeMessage('请选择要操作的数据', VxeNotifyType.warning);
    return;
  }
   proxy.$emit('clickCell', selectRows);


}
</script>
<style lang="scss" scoped>
#specSignHistory {
  height: 100%;

  // 附加按钮间距
  .button-blue,
  .button-red {
    margin: 5px 0 5px 5px;
  }
  .vxe-form {
    :deep(.vxe-form--item) {
      margin-right: 10px;
    }
  }
  .specSignHistoryTable {
    height: calc(100vh - 40px - 32px);
  }

  .returnModal {
    :deep(.vxe-modal--body) {
      height: 100%;
    }
  }
}

:deep(.vxe-textarea-height > textarea) {
  height: calc(40vh - 10px) !important;
}

.history-container {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.history-top-tool-box {
  flex: 0 0 auto;
}

.history-center-table-box {
  flex: 1 1 auto;
  overflow: auto;
}

.history-bottom-pagesize-box {
  flex: 0 0 auto;
}
</style>
